package cn.doitedu.webserver.service;

import cn.doitedu.webserver.dao.UserDao;
import cn.doitedu.webserver.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    UserDao userDao;

    @Override
    public boolean login(String account, String password) {

        User u = new User();
        u.setAccount(account);

        User user = userDao.findUserByAccount(u);
        if(user == null) return false;

        return user.getPassword()!=null && user.getPassword().equals(password)?true:false;
    }


    @Override
    public boolean register(String account, String password, String phoneNbr) {

        User user = new User();
        user.setAccount(account);
        user.setPassword(password);
        user.setPhone(phoneNbr);


        User userByAccount = userDao.findUserByAccount(user);
        if(userByAccount != null) return false;


        boolean b = false;
        try {
            b = userDao.insertUser(user);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return b;
    }
}
